package org.jprogger.myworld.sample.client;

import java.util.List;

import org.jprogger.myworld.api.app.ApiParams;
import org.jprogger.myworld.api.app.AppContext;
import org.jprogger.myworld.api.app.InitAppParams;
import org.jprogger.myworld.api.client.ApiClient;
import org.jprogger.myworld.api.model.Audio;
import org.jprogger.myworld.api.model.User;
import org.jprogger.myworld.api.response.ApiResponse;
import org.jprogger.myworld.api.service.AudioService;
import org.jprogger.myworld.api.service.FriendService;

public class SampleApiClient {
		
	public void getSimpleApiCall() throws Exception {
			
		List<User> friends = null;
		
		/*
		 *  Api parameters init for first time call
		 */
		ApiParams apiParams = new ApiParams();
		apiParams.setSecretKey("your_secret_key");
		apiParams.setFormat(ApiParams.JSON_REQUEST_FORMAT);
		apiParams.setIsRequestSecure(ApiParams.SECURE_REQUEST);
		
		/*
		 *  application parameters init for first time call
		 */
		InitAppParams appParams = new InitAppParams();
		appParams.setOwnerId("15564830132176373349");
		appParams.setViewerId("15564830132176373349");
		appParams.setSessionExpire("1274824244");
		appParams.setSessionKey("14f79f8373ec1c6a6fe5545d5bae9dd7");
		appParams.setWindowId("CometName_21608a1822633c43d4f31fb7d29b5be5");
		appParams.setApplicationId("441644");
		appParams.setAuthenticationKey("11a61504aa4426c816c5527ba8c89c75");
		
		/*
		 *  init application context for first time call
		 */
		AppContext.initContext(appParams, apiParams);
		AppContext context = AppContext.getInstance();
		
		
		ApiClient client = ApiClient.getApiClient(context);
		
		// TEST FRIEND SERVICE
		
		FriendService friendService = client.getFriendService();
		ApiResponse<List<User>> friendsResponse = 
			friendService.getFriends(context.getAppParams().getViewerId(), 1);
		
		if (!friendsResponse.hasError()) 
		{
			friends = friendsResponse.getResponseData().getData();
			if (friends != null) 
			{
				for (User user : friends) {
					System.out.println("user nickname: " + user.getNickName());
					System.out.println("user first name: " + user.getFirstName());
					System.out.println("user last name: " + user.getLastName());
					System.out.println("user birthday: " + user.getBirthday());
					System.out.println("user gender: " + user.getGender());
					System.out.println("user ID: " + user.getUseId());
					
					System.out.println("------------------------------------------");
				}
			}
		}
		
		// TEST AUDIO SERVICE
		
		AudioService audioService = client.getAudioService();
		ApiResponse<List<Audio>> audioResponse = audioService.getAudios(new int[0]);
		
		if (!friendsResponse.hasError())
		{
			List<Audio> audios = audioResponse.getResponseData().getData();
			if (audios != null)
			{
				for (Audio audio : audios) {
					System.out.println("audio id: " + audio.getId());
					System.out.println("audio owner: " + audio.getOwner());
					System.out.println("audio artist: " + audio.getArtist());
					System.out.println("audio title: " + audio.getTitle());
					System.out.println("audio size: " + audio.getSize());
					System.out.println("audio created time: " + audio.getCreatedTime());
					System.out.println("audio duration time: " + audio.getDurationTime());
					System.out.println("----------------------------");
				}
			}
		}
		
	}
		
	public static void main(String[] args) {
		SampleApiClient client = new SampleApiClient();
		try {
			client.getSimpleApiCall();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
